const {
  createType,
  findAllType,
  findType,
  updateType,
  removeType,
  findAllTypeList,
} = require("../service/type.service");

class TypeController {
  async create(ctx) {
    const { type_name } = ctx.request.body;

    const res = await createType({ type_name });

    ctx.body = {
      code: 0,
      message: "添加成功",
      result: res,
    };
  }

  async remove(ctx) {
    const id = ctx.request.params.id;

    const res = await removeType(id);

    ctx.body = {
      code: 0,
      message: "删除成功",
      result: res,
    };
  }

  async update(ctx) {
    const id = ctx.request.params.id;

    const res = await updateType(id, ctx.request.body);

    ctx.body = {
      code: 0,
      message: "更新成功",
      result: res,
    };
  }

  async findAll(ctx) {
    // 1. 解析pageNum和pageSize
    const { pageNum = 1, pageSize = 10, type_name } = ctx.request.body;
    // 2. 调用数据处理的相关方法
    const res = await findAllType({ pageNum, pageSize, type_name });
    // 3. 返回结果
    ctx.body = {
      code: 0,
      message: "获取列表成功",
      result: res,
    };
  }
  async findAllType(ctx) {
    // 2. 调用数据处理的相关方法
    const res = await findAllTypeList();
    // 3. 返回结果
    ctx.body = {
      code: 0,
      message: "获取列表成功",
      result: res,
    };
  }

  async findTypeByid(ctx) {
    const { id } = ctx.params;
    console.log(ctx.params, "ctx.params");
    const res = await findType({ id });
    if (res) {
      ctx.body = {
        code: 0,
        message: "获取成功",
        result: res,
      };
    } else {
      ctx.body = {
        code: 500,
        message: "id有误",
      };
    }
  }
}

module.exports = new TypeController();
